package dailimoshi.dongtaidaili;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UseJdkProxy {
    public static void main(String[] args) {
        Object proxyObject = getProxyObject(HelloWorld.class);
        ((Hello)proxyObject).sayHello();
    }


    private static Object getProxyObject(Class<?> clazz){
        Object o = Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
              /*  System.out.println("方法执行前");
                Object originObject = clazz.newInstance();
                Object result = method.invoke(originObject, args);
                System.out.println("方法执行后");
                return result;*/
                String hello = "hello";
                return null;
            }
        });
        return o;
    }

}

interface Hello{
    void sayHello();
}

class HelloWorld implements Hello{
    public void sayHello() {
        System.out.println("hello world");
    }
}

class HelloSea implements Hello{
    public void sayHello() {
        System.out.println("Hello sea");
    }
}
